<script>
import {delDoctor, deleteBatch, edit, list, save} from "@/api/patient";

export default {
  name: "Patient",
  data() {
    return {
      tableData: [],
      patientName: "",
      currentPage: 1,
      pageSize: 5,
      total: 0,
      loading: true,
      multipleSelection: [],
      dialogFormVisible: false,
      form: {user:{}},
      formLabelWidth: '150px',
      dialogFormVisible1: false,
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      let params={
        patientName:this.patientName,
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      }
      list(params).then(res => {
        this.tableData = res.data.records;
        this.loading = false;
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.load();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.load();
    },
    reset() {
      this.patientName="";
      this.currentPage = 1;
      this.pageSize = 5;
      this.total = 0;
      this.load();
    },
    handleDelete(id) {
      delDoctor(id).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            showClose: true,
            message:  res.msg,
            type: 'error'
          });
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((a) => a.id);
      console.log(this.multipleSelection);
    },
    handleBatchRemove() {
      let ids = this.multipleSelection;
      deleteBatch(ids).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            center: true,
            message:  res.msg,
            type: 'error'
          });
        }
      })
    },
    handleEdit(row) {
      this.form = row;
      this.dialogFormVisible1 = true;
    },
    edit() {
      edit(this.form).then(res=>{
        if (res.code === 200) {
          this.$message({
            message: '患者信息修改成功',
            type: 'success',
            center: true
          });
          this.dialogFormVisible1 = false;
          this.form = {user:{}};
          this.load();
        } else {
          this.$message({
            message: res.msg,
            type: 'error',
            center: true
          });
        }
      })
    },
    handleSave(){
      this.form={user:{}};
      this.dialogFormVisible=true;
    },
    save() {
      save(this.form).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '医生创建成功',
            type: 'success',
            center: true
          });
          this.dialogFormVisible = false;
          this.form = {user:{}};
          this.load();
        } else {
          this.$message({
            message:  res.msg,
            type: 'error',
            center: true
          });
        }
      })
    },
    headerBg({row,rowIndex}){
      if(rowIndex==0){
        return 'headerBgClass';
      }
    }
  }
}
</script>

<template>
  <div>
    <div class="search">
      <el-input style="width:160px" v-model="patientName" suffix-icon="el-icon-search" placeholder="请输入患者名称"></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
      <el-button type="warning" style="margin-left: 5px" @click="reset">重置</el-button>
    </div>
    <div style="margin:10px 0px" class="operation">
      <el-button type="primary" class="ml-5" icon="el-icon-circle-plus-outline" @click="handleSave">新增
      </el-button>
      <el-dialog title="新增患者" :visible.sync="dialogFormVisible" width="30%" center>
        <el-form :model="form" label-width="100px" style="width:340px">
          <el-form-item label="患者姓名" >
            <el-input v-model="form.user.userName" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="密码" >
            <el-input v-model="form.user.password" autocomplete="off" clearable show-password></el-input>
          </el-form-item>
          <el-form-item label="身份证号码" >
            <el-input v-model="form.idNumber" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="患者性别" >
            <el-radio-group v-model="form.user.sex">
              <el-radio :label="'0'">男</el-radio>
              <el-radio :label="'1'">女</el-radio>
              <el-radio :label="'2'">未知</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="患者邮箱" >
            <el-input v-model="form.user.email" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="患者电话" >
            <el-input v-model="form.user.phonenumber" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="患者家庭地址" >
            <el-input type="textarea" v-model="form.user.address" autocomplete="off" clearable></el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="save">确 定</el-button>
          <el-button @click="dialogFormVisible= false">取 消</el-button>
        </div>
      </el-dialog>
      <el-popconfirm
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="你确定批量删除吗？"
          @confirm="handleBatchRemove"
          class="ml-5"
      >
        <el-button type="danger" class="ml-5" icon="el-icon-remove-outline" slot="reference">批量删除</el-button>
      </el-popconfirm>
    </div>
    <div class="table">
      <el-table :data="tableData"
                border stripe
                v-loading="loading"
                max-height="550"
                :header-cell-class-name="headerBg"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(255,225,255, 0.1)"
                @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55px" align="center"></el-table-column>
        <el-table-column prop="id" label="ID" width="55px" align="center">
        </el-table-column>
        <el-table-column prop="user.userName" label="患者姓名" width="100px" align="center">
        </el-table-column>
        <el-table-column  label="患者头像" :show-overflow-tooltip=true align="center">
          <template slot-scope="scope">
            <div style="align-items: center">
              <el-image style="width:40px;height:40px;border-radius: 50%" v-if="scope.row.user.avatar" :src="scope.row.user.avatar" :preview-src-list="[scope.row.user.avatar]"></el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="idNumber" label="身份证号码" width="100px" align="center">
        </el-table-column>
        <el-table-column prop="birthDate" label="出生日期" width="100px" align="center">
        </el-table-column>
        <el-table-column  label="患者性别" width="70px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.user.sex=='2'">未知</span>
            <i v-else-if="scope.row.user.sex=='0'" class="el-icon-male"></i>
            <i v-else class="el-icon-female"></i>
          </template>
        </el-table-column>
        <el-table-column prop="user.email" label="患者邮箱"  align="center">
        </el-table-column>
        <el-table-column prop="user.phonenumber" label="患者电话"  align="center">
        </el-table-column>
        <el-table-column prop="user.address" label="患者家庭地址"  :show-overflow-tooltip="true" align="center">
        </el-table-column>
        <el-table-column prop="account" label="账户余额" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center" width="115px">
          <template slot-scope="scope">
            <el-button type="success" icon="el-icon-edit" style="margin-left: 5px;margin-bottom: 5px" @click="handleEdit(scope.row)">编辑</el-button>
            <el-dialog title="编辑患者信息" :visible.sync="dialogFormVisible1" width="30%" center>
              <el-form :model="form" label-width="100px" style="width:340px">
                <el-form-item label="患者姓名" >
                  <el-input v-model="form.user.userName" autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="身份证号码" >
                  <el-input v-model="form.idNumber" autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="患者性别" >
                  <el-radio-group v-model="form.user.sex">
                    <el-radio :label="'0'">男</el-radio>
                    <el-radio :label="'1'">女</el-radio>
                    <el-radio :label="'2'">未知</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="患者邮箱" >
                  <el-input v-model="form.user.email" autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="患者电话" >
                  <el-input v-model="form.user.phonenumber" autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="患者家庭地址" >
                  <el-input type="textarea" v-model="form.user.address" autocomplete="off" clearable></el-input>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="edit">确 定</el-button>
                <el-button @click="dialogFormVisible1 = false">取 消</el-button>
              </div>
            </el-dialog>
            <el-popconfirm
                class="ml-5"
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定删除吗？"
                @confirm="handleDelete(scope.row.id)"
            >
              <el-button type="danger" icon="el-icon-delete" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="padding:10px 0px">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<style scoped>
.headerBgClass{
  background-color: #cccccc !important;
}
</style>